package com.hiof.settings;

import android.content.SharedPreferences;
import android.util.Log;
import com.hiof.Configuration;

/**
 * Created by IntelliJ IDEA.
 * User: manish
 * Date: 3/22/12
 * Time: 10:16 PM
 * Setting set in preferences
 */
public class SettingManager {
    private static SettingManager instance;

    public static final String USER_NAME = "user_name";
    public static final String PASSWORD = "password";

    private static final String AUTO_LOGIN = "autologin";
    private static final boolean AUTO_LOGIN_DEFAULT_VALUE = true;

    private static final String SIMULATION_MODE = "simulationMode";
    private static final boolean SIMULATION_DEFAULT_VALUE = false;

    private static final String SIMULATION_SPEED = "speedForSimulation";
    private static final int DEFAULT_SPEED = 2;

    public static final short WILDCARD = 0;
    private static final String ANT_HRM_DEVICE_NO = "hrmDeviceNumber";
    private static final String PROXIMITY_THRESHOLD = "ProximityThreshold";
    private static final String BUFFER_THRESHOLD = "BufferThreshold";

    public static final String CURRENT_HEART_RATE = "BufferThreshold";
    public static final int DEFAULT_HEARTTATE = 72;

    /**
     * Skeleton method
     */
    public static SettingManager getInstance() {
        if (instance == null)
            instance = new SettingManager();
        return instance;
    }

    public void destroy() {
        if (instance != null) {
            instance = null;
        }

    }

    /**
     * Save auto login value from setting view into preferences
     *
     * @param value
     */
    public void setAutoLogin(boolean value) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(AUTO_LOGIN, value);
        // Commit the edits!
        editor.commit();
    }

    /**
     * Get auto login value If it in SharePreferences: get this value If not in
     * SharePreferences: use default value
     */
    public boolean getAutoLogin() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        boolean autoLogin = settings.getBoolean(AUTO_LOGIN,
                AUTO_LOGIN_DEFAULT_VALUE);
        return autoLogin;
    }


    /**
     * Save the simulation value from setting view into preferences
     *
     * @param value
     */
    public void setSimulation(boolean value) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(SIMULATION_MODE, value);
        editor.commit();
    }

    /**
     * Get whether the application is in simulation mode or not
     * if not set then use use default value: SIMULATION_DEFAULT_VALUE
     */
    public boolean getSimulation() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        boolean autoLogin = settings.getBoolean(SIMULATION_MODE,
                SIMULATION_DEFAULT_VALUE);
        return autoLogin;
    }

    /**
     * Save the simulation value from setting view into preferences
     *
     * @param value
     */
    public void setSimulationSpeed(int value) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(SIMULATION_SPEED, value);
        editor.commit();
    }

    /**
     * Get auto login value If it in SharePreferences: get this value If not in
     * SharePreferences: use default value
     */
    public int getSimulationSpeed() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        int speed = settings.getInt(SIMULATION_SPEED, DEFAULT_SPEED);
        return speed;
    }

    /**
     * Save username into preferences
     *
     * @param username
     */
    public void setUsername(String username) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(USER_NAME, username);
        // Commit the edits!
        editor.commit();
    }

    /**
     * Get username from SharePreferences get value
     */
    public String getUsername() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        String username = settings.getString(USER_NAME, "");
        return username;
    }

    /**
     * Save password into preferences, if you have time or ability try to
     * encrypt data
     *
     * @param password
     */
    public void setPassword(String password) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(PASSWORD, password);
        // Commit the edits!
        editor.commit();
    }

    /**
     * Get password from SharePreferences get value, if you have time or ability
     * try to decrypt data
     */
    public String getPassword() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        String password = settings.getString(PASSWORD, "");
        return password;
    }

    /**
     * Set user information on setting storage
     */
    public void setUserInfo(String username, String password, boolean autologin) {
        setAutoLogin(autologin);
        setUsername(username);
        setPassword(password);
    }

    public void setAntHRMDeviceNumber(int deviceNumber) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(ANT_HRM_DEVICE_NO, deviceNumber);
        editor.commit();
    }

    public int getAntHRMDeviceNumber() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        return settings.getInt(ANT_HRM_DEVICE_NO, WILDCARD);
    }

    public void setProximityThreshold(int deviceNumber) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(PROXIMITY_THRESHOLD, deviceNumber);
        editor.commit();
    }

    public int getProximityThreshold() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        return settings.getInt(PROXIMITY_THRESHOLD, WILDCARD);
    }

    public void setBufferThreshold(int deviceNumber) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(BUFFER_THRESHOLD, deviceNumber);
        editor.commit();
    }

    public int getBufferThreshold() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        return settings.getInt(BUFFER_THRESHOLD, WILDCARD);
    }

    public static void setCurrentHeartRate(int bpm) {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(CURRENT_HEART_RATE, bpm);
        editor.commit();
    }

    public int getCurrentHeartRate() {
        SharedPreferences settings = Configuration.getInstance().getPreferences();
        return settings.getInt(CURRENT_HEART_RATE, DEFAULT_HEARTTATE);
    }
}

